package com.lm56.web.network.service;

import com.lm56.common.exception.FieldValidateExcetiption;
import com.lm56.common.MyRequestBody;
import com.lm56.common.exception.MyException;
import com.lm56.web.CommonService;
import com.lm56.web.network.domain.*;
import com.lm56.web.network.mapper.LinesMapper;
import com.lm56.web.security.SecUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/11/27.
 */
@Component
@Transactional
public class LinesService {

    @Autowired
    LinesMapper linesMapper;

    @Autowired
    CommonService commonService;
    /**
     * 根据公司ID和查询参数获取公司路线列表
     *
     * @param param
     * @return
     */
    public List<CompanyLine> getCompanyLinesForCompany(MyRequestBody<CompanyLineQueryForm> param){
        return linesMapper.getCompanyLinesForCompany(param);
    }

    /**
     * 根据公司ID和查询参数获取公司路线列表
     *
     * @param param
     * @return
     */
    public int getCompanyLinesCount(CompanyLineQueryForm param){
        return linesMapper.getCompanyLinesCount(param);
    }

    /**
     * 根据公司ID和查询参数获取公司路线列表
     *
     * @param userId
     * @return
     */
    public CompanyLine getCompanyLinesById(Long userId, Long lineId){
        return linesMapper.getCompanyLinesById(userId, lineId);
    }

    /**
     * 插入线路数据
     *
     * @param line
     */
    public void addLine(CompanyLine line){

        //校验
        if(line.getStartLocation() == null || line.getStartLocation().getLocationId() ==null){
            throw new FieldValidateExcetiption("startLocation","请选择始发地点");
        }
        if(line.getEndLocation() == null || line.getEndLocation().getLocationId() ==null){
            throw new FieldValidateExcetiption("endLocation","请选择终点");
        }

        SecUserDetails secUser= (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        line.setUserId(secUser.getUserId());
        linesMapper.insertCompanyLine(line);
        List<CompanyLinePrice> prices = line.getPrices();
        for(CompanyLinePrice price:prices){
            price.setLineId(line.getId());
        }
        linesMapper.insertCompanyLinePrice(line.getPrices());

    }




    /**
     * 更新路线数据
     *
     * @param line
     */
    public void updateCompanyLine(CompanyLine line){
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //判断这条线路是否属于这个用户
        CompanyLine companyLine = linesMapper.getCompanyLinesById(secUser.getUserId(), line.getId());
        if(companyLine==null){
            throw new MyException("线路不存在");
        }

        line.setUserId(secUser.getUserId());
        linesMapper.updateCompanyLine(line);
        linesMapper.deleteCompanyLinePricedByLineId(line.getId());
        for(CompanyLinePrice price :line.getPrices()){
            price.setLineId(line.getId());
        }
        linesMapper.insertCompanyLinePrice(line.getPrices());

    }

    /**
     * 删除路线数据
     *
     * @param lineId
     */

    public void deleteCompanyLineById(long lineId){
        //选判断线路属不属于操作用户
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        CompanyLine line = linesMapper.getCompanyLinesById(secUser.getUserId(),lineId);
        if(line ==null){
            throw new MyException("线路不存在");
        }
        linesMapper.deleteCompanyLineById(lineId);
        linesMapper.deleteCompanyLinePricedByLineId(lineId);
    }
    /**
     * 删除路线价格数据
     *
     * @param lineId
     */

    public void deleteCompanyLinePricedByLineId(long lineId){

    }

    /**
     * 根据公司ID获取网点列表
     *
     * @param requestBody
     * @return
     */
    public List<CompanyNetwork> getNetworks(MyRequestBody<CompanyNetworkQeuryForm> requestBody){
       return linesMapper.getNetworks(requestBody);
    }


    /**
     * 根据公司ID获取网点列表
     *
     * @param param
     * @return
     */
    public int getNetworkCount(CompanyNetworkQeuryForm param){
        return linesMapper.getNetworkCount(param);
    }
    /**
     * 根据公司ID和网点ID获取指定网点
     * @param userId
     * @param networkId
     * @return
     */
    public CompanyNetwork getNetworkByNetworkId(Long userId,Long networkId){
        return linesMapper.getNetworkByNetworkId(userId,networkId);
    }


    /**
     * 插入网点数据
     *
     * @param network
     */
    public void addNetwork(CompanyNetwork network){
        //验证位置信息
        if(network.getLocation().getLocationId() == null){
            throw  new FieldValidateExcetiption("location","请选择所在地");
        }
        Location location =  commonService.getLocationById(network.getLocation().getLocationId());
        if(location== null){
            throw  new FieldValidateExcetiption("location","请选择所在地");
        }

        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        network.setUserId(secUser.getUserId());
        linesMapper.insertNetwork(network);

        int i = 1;
        for(CompanyNetworkContact contact: network.getContacts()){

            contact.setNetworkId(network.getNetworkId());
            contact.setSeqNo(i++);

        }
        linesMapper.insertNetworkContats(network.getContacts());
    }



    /**
     * 更新网点数据
     *
     * @param network
     */
    public void updateNetwork(CompanyNetwork network){
        if(network.getNetworkId()==null){
            throw new MyException("网点不存在");
        }
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        CompanyNetwork tmpNetwork = linesMapper.getNetworkByNetworkId(secUser.getUserId(),network.getNetworkId());
        if(tmpNetwork ==null){
            throw new MyException("网点不存在");
        }

        //验证位置信息
        if(network.getLocation().getLocationId() == null){
            throw  new FieldValidateExcetiption("location","请选择所在地");
        }
        Location location =  commonService.getLocationById(network.getLocation().getLocationId());
        if(location== null){
            throw  new FieldValidateExcetiption("location","请选择所在地");
        }

        network.setUserId(secUser.getUserId());
        linesMapper.updateNetwork(network);


        //删除联系人
        linesMapper.deleteNetworkContactByNetworkId(network.getNetworkId());
        int i = 1;
        for(CompanyNetworkContact contact: network.getContacts()){

            contact.setNetworkId(network.getNetworkId());
            contact.setSeqNo(i++);

        }
        linesMapper.insertNetworkContats(network.getContacts());

    }

    /**
     * 删除网点数据
     * @param networkId
     * @param networkId
     */
    public void deleteNetworkById(Long networkId) {

        if(networkId==null){
            throw new MyException("网点不存在");
        }
        SecUserDetails user= (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        CompanyNetwork tmpNetwork = linesMapper.getNetworkByNetworkId(user.getUserId(),networkId);
        if(tmpNetwork ==null){
            throw new MyException("网点不存在");
        }

        linesMapper.deleteNetworkById(user.getUserId(),networkId);
        linesMapper.deleteNetworkContactByNetworkId(networkId);
    }


}
